'use strict'
import { observable, action } from 'mobx'
import asyncFetch from '../config/fetch'
import config from '../config/config'
const listURL = config.resHeader + '/v1/doll/live/list'
class List {
  @observable list = []
  @observable banners = []
  page = 1
  listIsFetch = 0
  isFirst = true
  checkFirst = function () { if (this.isFirst) { window.Qapp.showLoad() } }
  @action getList = async function (cb) {
    if (this.listIsFetch) { return }
    this.listIsFetch = true
    if (cb) { this.page = 1 }
    this.checkFirst()
    try {
      const { page } = this
      const res = await asyncFetch(listURL, [{ page }])
      if (res.err === 0) {
        let arr = this.list.slice()
        if (cb) { arr = [] }
        const newArr = res.feeds
        this.list = arr.concat(newArr)
        this.banners = res.banners
        this.page += 1
      }
      else { window.Qapp.showToast(res.err_msg) }
    }
    catch (error) {
      console.log(error)
      window.Qapp.showToast('网络错误')
    }
    this.listIsFetch = false
    if (this.isFirst) {
      window.Qapp.hideLoad()
      this.isFirst = false
    }
    if (cb) { cb() }
  }
}

export default new List()
